package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 入住合同Service业务层处理
 *
 * @author alexis
 * @date 2025-01-02
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询入住合同
     *
     * @param id 入住合同主键
     * @return 入住合同
     */
    @Override
    public Contract selectContractById(Long id) {
        return contractMapper.selectById(id);
    }

    /**
     * 查询入住合同列表
     *
     * @param contract 入住合同
     * @return 入住合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增入住合同
     *
     * @param contract 入住合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract) {
        return contractMapper.insert(contract);
    }

    /**
     * 修改入住合同
     *
     * @param contract 入住合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract) {
        return contractMapper.updateById(contract);
    }

    /**
     * 批量删除入住合同
     *
     * @param ids 需要删除的入住合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids) {
        return contractMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除入住合同信息
     *
     * @param id 入住合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id) {
        return contractMapper.deleteById(id);
    }

    @Override
    public void updateContractStatus() {
        List<Contract> list = list(Wrappers.<Contract>lambdaQuery().le(Contract::getStartDate, LocalDateTime.now()).gt(Contract::getEndDate, LocalDateTime.now()).eq(Contract::getStatus, 0));
        if (list.isEmpty()) {
            return;
        } else {
            list.forEach(contract -> {
                contract.setStatus(1);
            });
        }
       updateBatchById(list);

    }
}
